﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace HML.Design
{
    /// <summary>
    /// AnomalyButton属性面板新增选项
    /// </summary>
    public class AnomalyButtonDesignerPropertyTab : PropertyTab
    {
        private static string ico = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAtdJREFUWEftls9LFGEYx7/PO5uHmNki9BAF7ph/QIjHDnnyEBQEeRASoyBnR6FOSQUZmWWZgbmzngKLEBYKukQUmYcQIjr0gw5CMyb9OFSgjlKUO0/M2q7u7uzuOO4iRHOc93mf5/M8z/t835ewwR9tcHz8uwD1Q9/DydCmGIQYsDrkV4UqXZEK1N6YVYUkmZmghCcg6rc65Me5IGUHqBuZbWRHeuGVMRHeAdRnavKd9HpZASLxuWZi8bDEwZ6wokpT2QHqjLlWhshkVgSiNMDO+LcdH7XqT35HVDXmugAx5NO+OIBq2BcBnGbG2LSutJZyGonb54jRU8BuAsDenLXCAKphnwVwYdWGKYAOWVH5tVcA1bDdrLu8g9MgwA1rAnAdqYbtOswuJ1Gbpcm304HcGV8KVcUJ8KrQTyI65p501bCfrhkgBRFbaAPxaHZmPPjLQV+InbCQpDiA5tzMGZgUDh8xO8NTf5MJBuBujowsHiDmBJir0oEYGCfmMIga88rOGLZ0JasdgSuQdl4bs5sEIQGgushhTBLTYVOXx3Jt1g3gOqwdXmwg4SQI2JWfNT+n30st5oltMwUOafAWZKpQBICAyRBJLVPaZk/NWHcF/LcALaau3CtrC7wOIYBnAGQAu/NuNeYrph4+tfp/4Ap4jyHdciTqlnhJZkfcBLDHo+8TSSSPzkS3pq7iQABeQsSEnmlNOb8iRHZNMgRXmPK0AMAPENotTUmsGSBfiukzHLRbnfkPiZRWGPZdAg56jygPACm98H8XLKugfQaEXmZ6MK3L+0pdRqqxMApwW1kuo8zoXf+6/cPJmi+lgqfXI4ZtEKD5tC/9HvDpKMssEpu/RETdPvZWBsANXBezo0yIlYCoHMDy6M3vB+h+AYi3xM5lU99SmUfpinzPNgghvVwF8UgQ+t9ryniegPnoWSCT+iFXK/gaSeKqeVx+U8hJWZ/lQUj/A/wB71gvMIFZMuQAAAAASUVORK5CYII=";

        /// <summary>
        /// PropertyGrid选项名称
        /// </summary>
        public override string TabName
        {
            get { return "视图设计器功能"; }
        }

        /// <summary>
        /// PropertyGrid选项图标
        /// </summary>
        public override Bitmap Bitmap
        {
            get
            {
                Bitmap image = null;
                using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(AnomalyButtonDesignerPropertyTab.ico)))
                {
                    image = (Bitmap)Image.FromStream(stream, true);
                }

                return image;
            }
        }

        public AnomalyButtonDesignerPropertyTab()
        {
        }

        public override PropertyDescriptorCollection GetProperties(object component)
        {
            return this.GetProperties(component, new Attribute[] { new AnomalyButtonDesignerPropertyAttribute(), new BrowsableAttribute(true) });
        }

        public override PropertyDescriptorCollection GetProperties(object component, Attribute[] attributes)
        {
            PropertyDescriptorCollection baseProperties = (attributes == null) ? TypeDescriptor.GetProperties(component) : baseProperties = TypeDescriptor.GetProperties(component, attributes);

            AnomalyButtonDesignerPropertyAttribute visibleAttribute = new AnomalyButtonDesignerPropertyAttribute();
            List<PropertyDescriptor> propertyList = new List<PropertyDescriptor>();
            for (int i = 0; i < baseProperties.Count; i++)
            {
                if (baseProperties[i].Attributes.Contains(visibleAttribute) && baseProperties[i].Attributes[typeof(BrowsableAttribute)] != null && ((BrowsableAttribute)baseProperties[i].Attributes[typeof(BrowsableAttribute)]).Browsable)
                {
                    propertyList.Add(baseProperties[i]);
                }
            }
            return new PropertyDescriptorCollection(propertyList.ToArray());
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attributes)
        {
            PropertyDescriptorCollection baseProperties = base.GetProperties(context, component, attributes);

            AnomalyButtonDesignerPropertyAttribute visibleAttribute = new AnomalyButtonDesignerPropertyAttribute();
            List<PropertyDescriptor> propertyList = new List<PropertyDescriptor>();
            for (int i = 0; i < baseProperties.Count; i++)
            {
                if (baseProperties[i].Attributes[typeof(AnomalyButtonDesignerPropertyAttribute)] != null && baseProperties[i].Attributes[typeof(BrowsableAttribute)] != null && ((BrowsableAttribute)baseProperties[i].Attributes[typeof(BrowsableAttribute)]).Browsable)
                {
                    propertyList.Add(baseProperties[i]);
                }
            }
            return new PropertyDescriptorCollection(propertyList.ToArray());
        }
    }

}
